<?php

namespace app\controller;

use Casbin\WebmanPermission\Permission;
use http\Exception\BadConversionException;
use support\Request;
use Tinywan\Jwt\JwtToken;

class IndexController
{
    public function index(Request $request)
    {
        static $readme;
        if (!$readme) {
            $readme = file_get_contents(base_path('README.md'));
        }
        return $readme;
    }

    public function view(Request $request)
    {
        $user_id = 'username';
        $roles = ['admin1','admin2','admin3','admin4','admin5'];

        //授权给用户
        // adds permissions to a user
        Permission::addPermissionForUser($user_id, 'articles', 'read');
        // adds a role for a user.
        //Permission::addRoleForUser('eve', 'writer');
        Permission::addRolesForUser($user_id,$roles);

        // adds permissions to a rule
        Permission::addPolicy($roles[0], 'articles','edit');

        if (Permission::enforce($user_id, 'articles', 'edit')) {
            echo '恭喜你！通过权限认证';
        } else {
            echo '对不起，您没有该资源访问权限';
        }

        return ['123'];

        return view('index/view', ['name' => '123']);
        //$startAt = \Ext\SeasLog::getMicroTime();
        ob_start();
        phpinfo();
        return nl2br(ob_get_clean());
        return view('index/view', ['name' => 'webman']);
    }

    public function returnJson(Request $request)
    {
        //return response(json(['code' => 0, 'msg' => 'ok','time'=>time()]))->withHeader('Content-Encoding', 'gzip');

        $diff = ['gmv'=>100000,'order_user_num'=>1000];

        $data = ['parent_id'=>'19','type'=>1,'data'=>[
            ['business_id'=>27,'gmv'=>'100000','order_user_num'=>'4000'],
            ['business_id'=>31,'gmv'=>'80000','order_user_num'=>'7000'],
            ['business_id'=>32,'gmv'=>'90000','order_fre'=>'4.6'],
            ['business_id'=>108,'gmv'=>'110000','order_user_num'=>'3000']
        ]];

        return json($data);

//        $data = ['parent_id'=>'19','type'=>1,'data'=>[
//            ['business_id'=>27,'data_list'=>['gmv'=>'100000','order_user_num'=>'4000']],
//            ['business_id'=>31,'data_list'=>['gmv'=>'80000','order_user_num'=>'7000']],
//            ['business_id'=>32,'data_list'=>['gmv'=>'90000','order_user_num'=>'8000']],
//            ['business_id'=>108,'data_list'=>['gmv'=>'110000','order_user_num'=>'3000']]
//        ]];

        $insert_data = [];

        foreach ($data['data'] as $value){
            $obj_data = [];
            $business_id = $value['business_id'];
            unset($value['business_id']);
            foreach ($value as $k=>$v){
                $obj_data[] = ['key'=>$k,'value'=>$v];
            }
            $insert_data[] = ['business_id'=>$business_id,'obj_data'=>json_encode($obj_data)];
        }

       // return json($insert_data);

        foreach ($diff as $key=>$value){
            $sum = array_sum(array_column($data['data'],$key));
            if(empty($sum)){
                continue;
            }
            if($sum != $value){
                return $key.':目标分配有差异';
            }
        }

        return json($data);
    }
    public function jwt(Request $request)
    {
        $user = [
            'id'  => 2022, // 这里必须是一个全局抽象唯一id
            'name'  => 'Tinywan',
            'email' => 'Tinywan@163.com'
        ];

        $token = JwtToken::generateToken($user);

        return json($token);

    }
    public function jwtCheck(Request $request)
    {
        $result = JwtToken::verify();
        return json($result);

    }

    public function flushJwtToken(Request $request){
        $result = JwtToken::refreshToken();
        return json($result);
    }

}
